

import { EosModel } from '@/utils/eos/eos';

class Model {
  constructor() {
    this.vthis = null;
    this.env = process.env.NODE_ENV;
    this.waxjs = null;
    this.paramsTransaction = null;
    this.paramsSign = null;
  }

  init(vthis, wax) {
    this.vthis = vthis;
    this.waxjs = wax;
  }

  // 登录
  async login(callback) {
    const userAccount = await this.waxjs.login();
    if (userAccount) {
      const identity = { accounts: [{
        name: userAccount,
        authority: 'active'
      }]
      }
      const item = {
        identity,
        wallet: 'cloudWallet',
        chain: 'wax'
      }
      EosModel.conserveAccount(item);
      if (this.paramsTransaction) {
        this.transaction(this.paramsTransaction, callback)
      } else if (this.paramsSign) {
        this.sign(this.paramsSign, callback)
      } else {
        callback('success');
      }
      return;
    }
    callback('error');
  }

  // transact -- 下单/签名
  transaction(params, callback) {
    if (!this.waxjs.api) {
      this.paramsTransaction = params;
      this.login(callback);
      return;
    }
    this.waxjs.api.transact(params, {
      broadcast: params.actions[0].name !== 'verify',
      blocksBehind: 3,
      expireSeconds: 1200,
    }).then(callback).catch(callback);
    this.paramsTransaction = null;
  }
}

// const WaxModel = new Model();

export const WaxModel = new Model();
export const WaxModel2 = new Model();
